package com.xm.xmds1705083.service.service.impl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.xm.xm.dto.ResultDto;
import com.xm.xm.dto.SearchDto;
import com.xm.xm.util.ErrorCode;
import com.xm.xmds1705083.service.mapper.DuoduoArticleMapper;
import com.xm.xmds1705083.service.model.DuoduoArticle;
import com.xm.xmds1705083.service.service.DuoduoArticleService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 杭州新苗网络科技有限公司
 * @author meihf
 * @date 2017/06/16
 * @description
*/ 
@Service("duoduoArticleService")
public class DuoduoArticleServiceImpl extends ServiceImpl<DuoduoArticleMapper,DuoduoArticle> implements DuoduoArticleService {

    @Autowired
    DuoduoArticleMapper duoduoArticleMapper;

    @Override
    public ResultDto findByType(SearchDto searchDto) {
        ResultDto resultDto = new ResultDto();
        searchDto.setPageNo((searchDto.getPageNo()-1)*searchDto.getPageSize());
        JSONObject filed = searchDto.getFiled();
        try {
            //1.常见问题帮助，2.关于我们',
             if (!filed.has("type")){
                  resultDto.setMsg(ErrorCode.E10011);
                  resultDto.setCode(ErrorCode.C10011);
                  return resultDto;
              }else{
                 EntityWrapper entityWrapper = new EntityWrapper();
                 entityWrapper.where(" type={0} ",filed.get("type")).orderBy("createTime",false);
                 List<DuoduoArticle> duoduoArticleList = duoduoArticleMapper.selectList(entityWrapper);
                 if (duoduoArticleList.size()==0){
                     resultDto.setCode(ErrorCode.C11001);
                     resultDto.setMsg(ErrorCode.E11001);
                     return resultDto;
                 }
                 resultDto.setData(duoduoArticleList);
              }

        }catch (RuntimeException e ){
            e.printStackTrace();
            resultDto.setCode(ErrorCode.C10013);
            resultDto.setMsg(ErrorCode.E10013);
        }
        return resultDto;
    }
}

